# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


class TutorialPipeline(object):
    #用来处理item事务
    fp = None
    #重写爬虫开始的方法
    def open_spider(self,spider):
        print("开始爬虫！！！")
        self.fp = open("./名言.txt",'w',encoding="utf-8")

    def process_item(self, item, spider):
        author = item['author']
        tags = item['tags']
        tags = "::::".join(tags)
        text = item['text']
        self.fp.write(author+":::"+text+":::"+tags+'\n')
        #return 是把item返回到下一个管道
        return item

    #重写爬虫结束的方法
    def close_spider(self,spider):
        print("关闭爬虫！！！")
        self.fp.close()
